<template>
  <div class="component-teamCalc">
    <div class="component-mainHeader">
      <div class="top">
        <div class="topleft">
          <p>
            <span class="iconfont" style="color:#1abc9a;margin-right:3px;">&#xe607;</span>施工公司信息
          </p>
        </div>
        <div class="topright">
          <el-button type="info" @click="$router.go(0)">
            <span style="font-weight:700;margin-right:5px;" class="iconfont">&#xe662;</span>刷新
          </el-button>
        </div>
      </div>
    </div>
    <div class="main">
      <div class="component-main-left">
        <div class="box1">
          <span class="iconfont">&#xe67e;</span>
          <span>{{editTitle}}</span>
          <div class="arrow"></div>
        </div>
      </div>
      <div class="component-main-right">
        <div class="box senInfo">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm exAddWarehouse"
          >
            <el-form-item label="施工公司名称:" prop="orgName">
              <el-input v-model="ruleForm.orgName"></el-input>
            </el-form-item>
            <el-form-item v-if="ms_roleName == 'WorkTeamManager'" label="施工队专业:" prop="checkList">
              <!--1-管道，2-线路，3-设备，4-熔纤，5-跳纤-->
              <el-checkbox-group v-model="ruleForm.checkList">
                <el-checkbox label="1">管道</el-checkbox>
                <el-checkbox label="2">线路</el-checkbox>
                <el-checkbox label="3">设备</el-checkbox>
                <el-checkbox label="4">熔纤</el-checkbox>
                <el-checkbox label="5">补盲扩</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="公司负责人:" prop="leaderName">
              <el-input v-model="ruleForm.leaderName" placeholder="请输入公司负责人"></el-input>
            </el-form-item>
            <el-form-item label="公司接单上限数:" prop="workOrderUplimit">
              <el-input v-model="ruleForm.workOrderUplimit"></el-input>
            </el-form-item>
            <el-form-item label="账号:" prop="userName">
              <el-input v-model="ruleForm.userName" :disabled="$route.params.flag==1?true:false"></el-input>
            </el-form-item>
            <el-form-item  v-if="$route.params.flag == 0" label="密码:" prop="userPwd">
              <el-input type="password" v-model="ruleForm.userPwd"></el-input>
            </el-form-item>
            <el-form-item  v-if="$route.params.flag == 1" label="新密码:" prop="newPwd">
              <el-input type="password" v-model="ruleForm.newPwd" placegolder="请输入新密码"></el-input>
            </el-form-item>
           <!-- <el-form-item label="所属角色:" prop="role">
              <el-input v-model="ruleForm.role"></el-input>
            </el-form-item>-->
            <el-form-item label="是否启用" prop="isUse">
              <el-switch v-model="ruleForm.isUse"></el-switch>
            </el-form-item>
            <el-form-item>
              <el-button style="margin-left:200px;" type="primary" @click="submitForm('ruleForm')">保存</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import area from '@/assets/js/area.js'
  export default {
    name:"teamCalc",
    data(){
      return {
        ms_roleName:localStorage.getItem("roleName"),
        editTitle:'添加施工公司',
        ruleForm: {
          orgName:'',
          checkList:[],
          leaderName:'',
          workOrderUplimit:'',
          userName:'',
          userPwd:'',
          role:'',
          isUse:true,
          newPwd:'',
        },
        rules: {
          orgName: [
            { required: true, message: "请输入施工公司名称", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          checkList: [
            { type: 'array',required: true, message: "请选择施工队专业", trigger: "change" },
          ],
          leaderName: [
            { required: true, message: "请输入公司负责人", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          workOrderUplimit: [
            { required: true, message: "请输入接单上限值", trigger: "blur" },
          ],
          userName: [
            { required: true, message: "请输入账号", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          userPwd: [
            { required: true, message: "请输入密码", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          role: [
            { required: true, message: "请输入所属角色", trigger: "blur" },
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ],
          newPwd:[
            { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
          ]
        },
        manageList:[],
        newArray:{}
      };
    },
    mounted(){
        console.log(this.$route.params.info)
//      this.manageLists();//管理员列表
      if(this.$route.params.flag == 0){
        this.editTitle = "添加施工公司"
      }else{
        console.log(this.$route.params.info);
        this.ruleForm = this.deepClone(this.$route.params.info);
        this.editTitle = "编辑施工公司"
      }
    },
    methods:{
      // 添加
      dataInit(){
        let that = this;
        let suCb = function(res){
          that.$message({
            message: '添加成功',
            type: 'success'
          });
          that.$router.push({name:'companyInfo'})
        };
        let erCb = function(res){
          console.log(res)
        };
        let userStatus = '0'
        if(that.ruleForm.isUse==true){
          userStatus = '1'
        }else{
          userStatus = '0'
        }
        let postData = {
            orgName: that.ruleForm.orgName,
            leaderName:  that.ruleForm.leaderName,
            workOrderUplimit:  that.ruleForm.workOrderUplimit,
            userName:  that.ruleForm.userName,
            userPwd:  "",
            userStatus:  userStatus
        };
        let palyParam = {
          url: "/api/web/ProConStructionManage/addConstructionCompany",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        };
        let pwd = that.deepClone(that.ruleForm);
        palyParam.data.userPwd = that.$md5(pwd.userPwd);
        that.$https(palyParam)
      },
      //编辑
      editInfo(){
        let that = this;
        let suCb = function(res){
          that.$message({
            message: '编辑成功',
            type: 'success'
          });
          that.$router.push({name:'companyInfo'})
        };
        let erCb = function(res){
          console.log(res)
        };
        let userStatus = '0'
        if(that.ruleForm.isUse==true){
          userStatus = '1'
        }else{
          userStatus = '0'
        }
        let postData = {
            orgName: that.ruleForm.orgName,
            leaderName:  that.ruleForm.leaderName,
            workOrderUplimit:  that.ruleForm.workOrderUplimit,
            userName:  that.ruleForm.userName,
            userPwd:  "",
            userStatus:  userStatus,
            orgId:that.$route.params.info.orgId,
            userId:that.$route.params.info.userId
        };
        let palyParam = {
          url: "/api/web/ProConStructionManage/updateConstructionCompany",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        };
        console.log(that.ruleForm.newPwd,111)
        if(that.ruleForm.newPwd == "" || that.ruleForm.newPwd==undefined){
          let pwd = that.deepClone(that.ruleForm);
          palyParam.data.userPwd = pwd.userPwd;
        }else{
          palyParam.data.userPwd = that.$md5(that.ruleForm.newPwd);
        }
        that.$https(palyParam)
      },
      //表单提交
      submitForm(formName){
        this.$refs[formName].validate(valid => {
          if (valid) {
            if(this.$route.params.flag == 1){
              this.editInfo();
            }else{
              this.dataInit();
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      //查询管理员
      manageLists(){
        let that = this;
        let suCb = function(res){
          that.manageList = res.data.data;
          that.newArray = that.manageList.find((item) => {
            if(item.managerId === that.$route.params.info.managerId){
              return item
            }
          });
          console.log(that.newArray)
          console.log(res.data)
        }
        let erCb = function(res){
          console.log(res)
        }
        let postData = {
        }
        let palyParam = {
          url: "/api/web/sysStore/selectManagerName",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        }
        that.$https(palyParam)
      }
    }
  }
</script>

<style scoped>
  .main{
    overflow: hidden;
    width: 1100px;
    margin: 1px auto;
    border: 1px solid #e4e4e4;
    border-left:none;
    border-top: none;
  }
  .component-main-left {
    width: 180px;
    border: 1px solid #e4e4e4;
    padding-top: 50px;
    float: left;
    height: 673px;
    border-top: none;
    border-bottom: none;
  }
  .box1 {
    position: relative;
    width: 175px;
    height: 50px;
    text-align: left;
    line-height: 50px;
    background-color: #9ea7b4;
    color: #fff;
    padding-left: 5px;
  }
  .box1 .arrow {
    position: absolute;
    width: 0;
    height: 0;
    top: -40px;
    left: 180px;
    border-width: 25px 0 25px 25px;
    border-style: solid;
    border-color: transparent transparent transparent #9ea7b4; /*透明 透明 透明 灰*/
    margin: 40px auto;
  }
  .component-main-right{
    float: left;
  }
  .box{
    margin-left: 120px;
    margin-top: 80px;
    outline: none;
  }
  .el-button--primary{
    background-color: #1ABC9C;
    border-color: #1ABC9C;
  }

</style>
